﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chowpoo.Common;
using Chowpoo.Util;
using CourseSystem.Common;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using CourseSystem.Core.Enum;
using CourseSystem.Stat.ActionFilter;
using Webdiyer.WebControls.Mvc;

namespace CourseSystem.Web.Controllers
{
    [UserAuth(UserType = UserTypeEnum.Guess)]
    public class CommentController : BaseController
    {
        //
        // GET: /Comment/
        [StatFilter]
        [UrlStatFilter]
        public ActionResult Index()
        {
            if (UserHelper.LoginUser.UserType == UserTypeEnum.Teacher && !UserHelper.LoginUser.IsExpert)
                return NotFound();
            if (UserHelper.LoginUser.UserType == UserTypeEnum.Admin)
                return NotFound();
            /* type = type.CheckInt(0);
             ViewData["type"] = type;
             if (type == 0)
             {*/
            return View();
            //  }
            /*if (type == 1)
            {
                return RedirectToAction("Expert");
            }*/
            //return NotFound();
        }
        /*
        [StatFilter]
        public ActionResult Expert()
        {
            ViewData["type"] = 1;
            return View();
        }
        */
        public ActionResult DetailsUc(int id)
        {
            var info = CourseCommentBLL.Get(id);
            if (info == null)
                return NotFound();
            return PartialView(info);
        }

        public ActionResult CommentListUc(int? pid)
        {
            CommonRequest.ClearCache();
            var list = CourseCommentBLL.GetList(1, 0, -1, 0).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView("CourseCommentListUc", model);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="commentContent"></param>
        /// <param name="type">0教师1课程</param>
        /// <param name="rankHid"></param>
        /// <param name="teacherId"></param>
        /// <param name="reviewerName"></param>
        /// <returns></returns>
        public ActionResult CommentEdit(string commentContent, int type, int rankHid, int? teacherId, string reviewerName)
        {
            teacherId = teacherId.CheckInt(0);
            if (string.IsNullOrEmpty(commentContent))
            {
                SetAlertMsg("请填写评价内容!", true);
                return RedirectToAction("Index");
            }
            var now = DateTime.Now;
            if (!UserHelper.LoginUser.IsExpert && UserHelper.LoginUser.UserType != UserTypeEnum.Student && UserHelper.LoginUser.UserType != UserTypeEnum.Guess)
            {
                SetAlertMsg("登录用户非游客身份或学生身份或专家身份!", true);
                return RedirectToAction("Index");
            }
            if (type == 0 && teacherId.Value == 0)
            {
                SetAlertMsg("请选择评价教师!", true);
                return RedirectToAction("Index");
            }
            var add = new CourseCommentInfo { Comment = commentContent, CommentTime = now, CommentType = type, Rank = rankHid };
            if (type == 0)
            {
                add.TeacherId = teacherId.Value;
                var teaInfo = TeacherBLL.Get(teacherId.Value);
                if (teaInfo == null)
                {
                    SetAlertMsg("请选择评价教师!", true);
                    return RedirectToAction("Index");
                }
                add.TeacherName = teaInfo.Name;
            }
            var reType = 0;
            if (UserHelper.LoginUser.UserType == UserTypeEnum.Student)
                reType = 0;
            else if (UserHelper.LoginUser.IsExpert)
                reType = 1;
            else reType = -1;
            add.ReviewerId = UserHelper.LoginUser.Id;
            add.ReviewerType = reType;
            add.ReviewerName = UserHelper.LoginUser.UserName;

            if (CourseCommentBLL.Add(add))
            {
                SetAlertMsg("评价成功!", true);
                return RedirectToAction("Index");
            }
            SetAlertMsg("未知错误!", true);
            return RedirectToAction("Index");
        }
    }
}
